package com.easylinkin.linkappapi.meterbilling.service;

import com.easylinkin.linkappapi.meterbilling.entity.ResidentInfo;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import site.morn.framework.context.LoginValidator;
import site.morn.util.BeanFunctionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Objects;

/**
 * class info :
 *
 * @author liuqihang
 * @date 2021/7/29 15:53
 */
@Service
public class MeterLoginService implements UserDetailsService {

    @Resource
    private ResidentService residentService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        ResidentInfo residentUser = residentService.getResidentInfoByPhone(s);
        if (Objects.isNull(residentUser)) {
            throw new UsernameNotFoundException("User no found:" + s);
        }
        BeanFunctionUtils.validates(LoginValidator.class, residentUser);

        return new User(residentUser.getPhone(),
                residentUser.getPassword(), true, true, true, true, Collections.emptyList());
    }
}
